# Created by: Savichev Alexandr <savicheval@gmail.com>
# $FreeBSD$

PORTNAME=               zabbix3x
PORTVERSION=            3.4.15
PORTREVISION?=          0
MASTER_SITES=           https://cdn.zabbix.com/zabbix/sources/oldstable/3.4/${PORTVERSION:R}/
CATEGORIES=             net-mgmt
DISTNAME=               zabbix-${PORTVERSION}

MAINTAINER=             alexei.vladishev@zabbix.com
COMMENT=                Enterprise-class open source distributed monitoring

PLIST=                  pkg-plist

OPTIONS_DEFINE=         CURL IPV6
OPTIONS_DEFAULT=        CURL OPENSSL


LICENSE=                GPLv2+
LICENSE_FILE=           ${WRKSRC}/COPYING

CONFLICTS_INSTALL=      zabbix[0-9][0-9]-agent zabbix[0-24-9]-agent

USE_RC_SUBR=            zabbix_agentd

USES=                   pkgconfig iconv

USERS=                  zabbix
GROUPS=                 zabbix

LIB_DEPENDS+=           libpcre.so:devel/pcre

MAKE_ARGS+=             ARCH=freebsd
GNU_CONFIGURE=          yes
CONFIGURE_ARGS+=        --enable-agent --sysconfdir=${ETCDIR} --datadir=${ETCDIR} --with-iconv=${ICONV_PREFIX}

OPTIONS_SINGLE=         SSL
OPTIONS_SINGLE_SSL=     OPENSSL GNUTLS

CURL_DESC=              Support for web monitoring
CURL_CONFIGURE_WITH=    libcurl
CURL_LIB_DEPENDS=       libcurl.so:ftp/curl

IPV6_CONFIGURE_ENABLE=  ipv6

NMAP_RUN_DEPENDS=       nmap:security/nmap sudo:security/sudo

LIBXML2_CONFIGURE_WITH= libxml2=${LOCALBASE}
LIBXML2_LIB_DEPENDS=    libxml2.so:textproc/libxml2

OPENSSL_CONFIGURE_WITH= openssl
OPENSSL_USES=           ssl
OPENSSL_VARS=           BROKEN_SSL="libressl libressl-devel" BROKEN_SSL_REASON="PSK is not provided by LibreSSL"

GNUTLS_CONFIGURE_WITH=  gnutls
GNUTLS_LIB_DEPENDS=     libgnutls.so:security/gnutls

.include <bsd.port.pre.mk>

post-patch:
        @${GREP} -rl '/etc/zabbix' ${WRKSRC} | ${XARGS} ${REINPLACE_CMD} -e 's#/etc/zabbix#${ETCDIR}#g'

        @${REINPLACE_CMD} -e 's#/usr/sbin/fping#${LOCALBASE}/sbin/fping#g' \
                ${WRKSRC}/conf/zabbix_*.conf \
                ${WRKSRC}/src/zabbix_proxy/proxy.c \
                ${WRKSRC}/src/zabbix_server/server.c

        @${REINPLACE_CMD} -e 's#PidFile=/tmp/zabbix_agentd.pid#PidFile=/var/run/zabbix/zabbix_agentd.pid#g' \
                ${WRKSRC}/conf/zabbix_agentd*.conf

        @${REINPLACE_CMD} -e 's#Include=/usr/local/usr/local/etc/zabbix3#Include=/usr/local/etc/zabbix3/zabbix#g' \
                ${WRKSRC}/conf/zabbix_agentd*.conf

        @${REINPLACE_CMD} \
                -Ee 's|(@sysconfdir@/(\$$\([A-Z]*_CONFIG\)))" ([|][|] cp "../../conf/\$$\([A-Z]*_CONFIG\)" "\$$\(DESTDIR\)@sysconfdir@)"|\1.sample" \3/\2.sample"|;' \
                ${WRKSRC}/src/zabbix_agent/Makefile.in

        @${FIND} ${WRKSRC} -type f \( -name '*.bak' -or -name '*.orig' \) \
                -exec ${RM} {} +

pre-configure:
        @echo "ac_cv_env_PKG_CONFIG_set=${LOCALBASE}/bin/pkgconf" > ${WRKSRC}/config.cache

post-install:

.include <bsd.port.pre.mk>

.include <bsd.port.post.mk>
